
const Seller = require('../model/Seller');
const Buller = require('../model/Buller');
const Good = require('../model/Good');
const Follow = require('../model/Follow');
const Like = require('../model/Likes');
const Desc = require('../model/Desc');
const Reply = require('../model/Reply');
const Message = require('../model/Message');
const Ding = require('../model/Ding');
const setting = require('../setting')
exports.clearDatabase = (req,res,next)=>{

	if(req.query.pwd != '131869'){
		return res.end('验证失败！');
	}

	Buller.find().exec(function (err,data) {
		if(data.length>0){


		data.forEach(function(item){

            Buller.remove({'_id':item._id}).then((err,data)=>{
				console.log(err);
			});

		})
        req.session.buller = null;
        res.clearCookie(setting.user_cookie_name); // 清空cookie
        req.session.bullerLogin = false;
        }

    })
    Seller.find().exec(function (err,data) {
        if(data.length>0) {
            data.forEach(function (item) {

                Seller.remove({'_id': item._id}).then((err, data) => {
                    console.log(err);
                });

            })
            req.session.seller = null;
            res.clearCookie(setting.auth_cookie_name); // 清空cookie
            req.session.sellerLogin = false;
        }

    })
    Good.find().exec(function (err,data) {
        if(data.length>0) {
            data.forEach(function (item) {

                Good.remove({'_id': item._id}).then((err, data) => {
                    console.log(err);
                });

            })
        }

    })
    Follow.find().exec(function (err,data) {
        if(data.length>0) {
            data.forEach(function (item) {

                Follow.remove({'_id': item._id}).then((err, data) => {
                    console.log(err);
                });

            })
        }

    })
    Like.find().exec(function (err,data) {
        if(data.length>0) {
            data.forEach(function (item) {

                Like.remove({'_id': item._id}).then((err, data) => {
                    console.log(err);
                });

            })
        }

    })

    Desc.find().exec(function (err,data) {
        if(data.length>0) {
            data.forEach(function (item) {

                Desc.remove({'_id': item._id}).then((err, data) => {
                    console.log(err);
                });

            })
        }
    })
    Message.find().exec(function (err,data) {
        if(data.length>0) {
            data.forEach(function (item) {

                Message.remove({'_id': item._id}).then((err, data) => {
                    console.log(err);
                });

            })
        }

    })
    Reply.find().exec(function (err,data) {
        if(data.length>0) {
            data.forEach(function (item) {

                Reply.remove({'_id': item._id}).then((err, data) => {
                    console.log(err);
                });

            })
        }

    })
    Ding.find().exec(function (err,data) {
        if(data.length>0) {
            data.forEach(function (item) {

                Ding.remove({'_id': item._id}).then((err, data) => {
                    console.log(err);
                });

            })
        }

    })
	// Good.remove();
	// Follow.remove();
	// Like.remove();
	// Desc.remove();
	// Reply.remove();
	// Message.remove();
	// Ding.remove();
	res.end('success');



}

exports.index = (req, res, next) => {
  res.render('index', {
    title: '首页',
    guanli:{}

  });

};


exports.login = (req, res, next) => {
  res.render('login', {
    title: '登录',
    guanli:{}

  });

};


exports.regist = (req, res, next) => {
  res.render('regist', {
    title: '注册',
    guanli:{}

  });

};
exports.goods = (req, res, next) => {
  
  
  
  Good.getAllGoods((err,goods)=>{
  	if(err){
  		return res.render('error',{
  			error:'',
  			message:'Not Fount'
  		})
  	}
  	if(goods.length>0){
        res.render('goods', {
            title: '商品管理',
            guanli:{},
            goods

        });
  	}else{
        res.render('goods', {
            title: '商品管理',
            guanli:{},
            goods:[]

        });
	}

  	
  	
  })

};
exports.seller = (req, res, next) => {
  Seller.getAllSeller((err,sellers)=>{
  	
  	if(err){
  		return res.render('error',{
  			error:'',
  			message:'Not Fount'
  		})
  	}



      if(sellers.length>0){
          res.render('seller', {
              title: '商家管理',
              guanli:{},
              sellers

          });
      }else{
          res.render('seller', {
              title: '商家管理',
              guanli:{},
              sellers:[]

          });
      }
  	
  	
  	
  })

};
exports.buller = (req, res, next) => {
  Buller.getAllBuller((err,bullers)=>{
  	
  	if(err){
  		return res.render('error',{
  			error:'',
  			message:'Not Fount'
  		})
  	}



      if(bullers.length>0){
          res.render('buller', {
              title: '用户管理',
              guanli:{},
              bullers

          });
      }else{
          res.render('buller', {
              title: '用户管理',
              guanli:{},
              bullers:[]

          });
      }
  	
  	
  	
  })

};


exports.postRegist = (req, res, next) => {
  
  console.log(req.body);
  

};
exports.postLogin = (req, res, next) => {
  
  console.log(req.body);
  

};
exports.disBuller = (req,res,next)=>{
	var id = req.query.id;
	Buller.getBullerById(id,(err,buller)=>{
		
		if(err){
			return res.end(err);
		}
		buller.isDis = !buller.isDis;
		buller.save();
		res.end('success');
		
	})
	
	
	
	
}
exports.disSeller = (req,res,next)=>{
	var id = req.query.id;
	Seller.getSellerById(id,(err,seller)=>{
		
		if(err){
			return res.end(err);
		}
		seller.isDis = !seller.isDis;
		seller.save();
		res.end('success');
		
	})
	
	
	
	
}

exports.removeGood = (req,res,next)=>{
	var id = req.query.id;
	
	Good.remove({'_id':id}).then((data)=>{
		res.end('success')
	}).catch((err)=>{
		res.render('error',{
  			error:'',
  			message:'Not Fount'
  		})
	})
	
	
}

